//
//  LoginViewController.m
//  FamilyMainProject
//
//  Created by WorkSpace on 3/31/14.
//
//

#import "LoginViewController.h"
#import "LSButton.h"
@interface LoginViewController ()

@end
//
@implementation LoginViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
    [self.nextButton setTimeInterval:0.1 maxAlpha:1 lessAlpha:0.2 repeats:YES];

}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)nextButton:(id)sender
{
    [sender stopFlicker];
    customAlertView = [[CustomIOS7AlertView alloc] init];
    [customAlertView setContainerView:[self createDemoView]];
    [customAlertView setButtonTitles:[NSMutableArray arrayWithObjects:@"登录", nil]];
    [customAlertView setDelegate:self];
    [customAlertView setUseMotionEffects:YES];
    //        [alertView setOnButtonTouchUpInside:^(CustomIOS7AlertView *alertView, int buttonIndex) {
    //            NSLog(@"Block: Button at position %d is clicked on alertView %d.", buttonIndex, [alertView tag]);
    //            [alertView close];
    //        }];
    [customAlertView setUseMotionEffects:true];
    [customAlertView show];
}

- (IBAction)newUsers:(id)sender
{
    
}
- (UIView *)createDemoView
{
    UIView *demoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 290, 300)];
    
    self.userName = [[UITextField alloc]initWithFrame:CGRectMake(30, 180, 230, 30)];
    self.userPassWord = [[UITextField alloc]initWithFrame:CGRectMake(30, 220, 230, 30)];
    self.userName.placeholder = @"Enter your userID(账号)";
    self.userPassWord.placeholder = @"Enter your password(密码)";
    self.userName.backgroundColor = [UIColor whiteColor];
    self.userPassWord.backgroundColor = [UIColor whiteColor];
    self.userName.borderStyle = UITextBorderStyleRoundedRect;
    self.userPassWord.borderStyle = UITextBorderStyleRoundedRect;
    //
    UIButton *forgotPasswork = [[UIButton alloc]initWithFrame:CGRectMake(95, 260, 100, 30)];
    [forgotPasswork setTitle:@"忘记密码" forState:UIControlStateNormal];
    [forgotPasswork.titleLabel setFont:[UIFont fontWithName:@"HelveticaNeue-UltraLight" size:12]];
    [forgotPasswork addTarget:self action:@selector(forgotPasswordAct) forControlEvents:UIControlEventTouchDown];
    
    UILabel *welcome = [[UILabel alloc]initWithFrame:CGRectMake(50, 10, 190, 100)];
    welcome.text = @"WELCOME";
    welcome.font = [UIFont fontWithName:@"Chalkduster" size:40];
    welcome.adjustsFontSizeToFitWidth = YES;
    welcome.textAlignment = 1;
    UILabel *welcome2 = [[UILabel alloc]initWithFrame:CGRectMake(50, 80, 190, 70)];
    welcome2.textAlignment = 1;
    welcome2.text = @"Itralver";
    welcome2.font = [UIFont fontWithName:@"Chalkduster" size:30];
    welcome2.adjustsFontSizeToFitWidth = YES;
    
    [demoView addSubview:welcome];
    [demoView addSubview:welcome2];
    [demoView addSubview:forgotPasswork];
    [demoView addSubview:self.userName];
    [demoView addSubview:self.userPassWord];
    self.userPassWord.delegate = self;
    self.userName.delegate = self;
    
    return demoView;
}
- (void)forgotPasswordAct
{
    
}
- (void)customIOS7dialogButtonTouchUpInside: (CustomIOS7AlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    userDefaults = [NSUserDefaults standardUserDefaults];
//    Lslog(@"Button Clicked! %d, %d", buttonIndex, [customAlertView tag]);
    if (self.userName.text.length==0)
    {
        [self popUIAlertView:nil andMessage:@"请填写您的账号"];
        return;
    }
    if (self.userPassWord.text.length==0)
    {
        [self popUIAlertView:nil andMessage:@"请填写您的密码"];
        return;
    }
    getNonce = [[PortPackaging alloc]init];

    [getNonce getNonceMethod:@"generate_auth_cookie" andController:@"auth" andJson:@"core.get_nonce" block:^(NSMutableDictionary *ReceiveData)
    {
        LSLog(@"nonec = %@",ReceiveData);
        [self.nextButton startFlicker];
        [customAlertView close];
        [self networkRequestForGetcookie];
        

    }
    failblock:^(NSError *error)
    {
        [self popUIAlertView:@"错误" andMessage:[NSString stringWithFormat:@"%@",[error localizedDescription]]];
    }];
    
}
- (void)myTask {
	// Do something usefull in here instead of sleeping ...
	sleep(3);
}

- (void)networkRequestForGetcookie
{
    getCookie = [[PortPackaging alloc]init];
    [getCookie getCookieName:self.userName.text withpwd:self.userPassWord.text block:^(NSMutableDictionary *ReceiveData)
     {
         if (![[ReceiveData objectForKey:@"status"]isEqualToString:@"ok"])
         {
             [self popUIAlertView:nil andMessage:@"账号或密码错误，请重新输入"];
             return ;
         }
         model = [UsersModel getDictionaryToObject:[ReceiveData objectForKey:@"user"]];
         LSLog(@"UsersModel = %@",model);
         [self networkRequestForLogin:[ReceiveData objectForKey:@"cookie"]];
     }
     failblock:^(NSError *error)
     {
         ;
     }];
}
- (void)networkRequestForLogin:(NSString *)cookie
{
    getLogin = [[PortPackaging alloc]init];
    [getLogin loginToCookie:cookie block:^(NSMutableDictionary *ReceiveData)
    {
        LSLog(@"Login = %@",ReceiveData);
    }
    failblock:^(NSError *error)
    {
        ;
    }];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField//textFiled的代理功能（具体效果百度去）
{
    if (textField == self.userName && [self.userPassWord.text isEqualToString:@""] && ![self.userName.text isEqualToString:@""])
    {
        [self.userPassWord becomeFirstResponder];
    }
    else if (textField == self.userPassWord && [self.userName.text isEqualToString:@""] && ![self.userPassWord.text isEqualToString:@""])
    {
        [self.userName becomeFirstResponder];
    }
    else
    {
        [textField resignFirstResponder];
    }
    return YES;
}

- (void)popUIAlertView:(NSString*)title andMessage:(NSString *)message
{
    UIAlertView *error = [[UIAlertView alloc]initWithTitle:title message:message delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [error show];
}
@end
